//
//  HJGroupMemberContentCell.m
//  KaiChat
//
//  Created by lx on 2023/11/6.
//

#import "HJGroupMemberContentCell.h"
#import "HJCollectionViewCell.h"
#import "HJGroupMemberModel.h"
#import "HJChatGroupModel.h"

@interface HJGroupSettingMemberCell: HJCollectionViewCell

@property (nonatomic, weak) UIImageView *avatarImageView;
@property (nonatomic, weak) UILabel *nameLabel;
@property (nonatomic, strong) HJGroupMemberModel *model;


@end

@implementation HJGroupSettingMemberCell


- (void)settingSubView {
    [super settingSubView];

    UIImageView *avatarImageView = [UIImageView addToView:self.contentView];
    avatarImageView.addCornerRadius(20);
    avatarImageView.contentMode = UIViewContentModeScaleAspectFill;
    [avatarImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(6);
        make.centerX.offset(0);
        make.width.height.offset(40);
    }];
 
    UILabel *nameLabel = [UILabel lableWithSuperView:self.contentView font:UIFontMake(12) color:UIColor.tj_333333Color title:nil textAlignment:NSTextAlignmentCenter];
    [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.top.equalTo(avatarImageView.mas_bottom).offset(4);
    }];
 
    _avatarImageView = avatarImageView;
    _nameLabel = nameLabel;
}

- (void)setModel:(HJGroupMemberModel *)model {
    _model = model;

    if ([model.userId isEqualToString:@"add"] || [model.userId isEqualToString:@"remove"]) {// + 1
        _nameLabel.text = nil;
        _avatarImageView.image = [UIImage imageNamed:model.avatar];
    } else {
        _nameLabel.text = model.nickname;
        if (isEmptyString(model.avatar)) {
            _avatarImageView.image = [UIImage imageNamed:defaultAvatarName];
        } else {
            [_avatarImageView sd_setImageWithURL:model.avatar.URL placeholderImage:[UIImage imageNamed:defaultAvatarName]];
        }
        
    }
}

 

@end

@interface HJGroupMemberContentCell()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UICollectionViewFlowLayout *flowLayout;
@property (nonatomic, strong) NSArray *datas;
@property (nonatomic, weak) UILabel *titleLabel;

@end


@implementation HJGroupMemberContentCell

- (void)settingSubView {
    [super settingSubView];
    

    UILabel *titleLabel = [UILabel lableWithSuperView:self.contentView font:UIFontMediumMake(14) color:UIColor.tj_333333Color title:nil textAlignment:NSTextAlignmentLeft];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(16);
        make.left.offset(leftRightMargin);
    }];
    
    [self addSubview:self.collectionView];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(titleLabel.mas_bottom);
        make.left.right.bottom.offset(0);
    }];
    
    _titleLabel = titleLabel;
}

 
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.datas.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    HJGroupSettingMemberCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HJGroupSettingMemberCell" forIndexPath:indexPath];
    HJGroupMemberModel *model = self.datas[indexPath.row];
    cell.model = model;
    return cell;
}

- (UICollectionView *)collectionView {
    if (!_collectionView) {
        
        UICollectionViewFlowLayout *flowLayout= [[UICollectionViewFlowLayout alloc]init];
        flowLayout.minimumLineSpacing = 8;
        flowLayout.minimumInteritemSpacing = 0;
        flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
        _flowLayout = flowLayout;
        flowLayout.sectionInset = UIEdgeInsetsMake(14, leftRightMargin, 0, leftRightMargin);
        flowLayout.itemSize = CGSizeMake((KScreenWidth -  leftRightMargin * 2 - flowLayout.minimumLineSpacing * 4)/ 5.0 - 0.5, 70);
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
        [_collectionView registerClass:HJGroupSettingMemberCell.class forCellWithReuseIdentifier:@"HJGroupSettingMemberCell"];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.alwaysBounceHorizontal=YES;
        
    }
    return _collectionView;
}

- (void)setModel:(HJCellModel *)model {
    _model = model;
    
    
    _titleLabel.text = model.text;
    
    NSArray *datas = model.anyObject;
    self.datas = datas;
    [self.collectionView reloadData];
}
 
- (void)dealloc {
    NSLog(@"%s", __func__);
}

@end

 
